package com.yjps.house.controller;


import com.yjps.common.message.Message;
import com.yjps.house.service.UserFrontService;
import com.yjps.system.model.Role;
import com.yjps.system.model.User;
import com.yjps.system.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;


@Controller
@RequestMapping("/house")
public class UserFrontController {

    @Autowired
    UserFrontService userFrontService;

    @Autowired
    RoleService roleService;

    /**
     *
     * @param mobile
     * @param password
     * @return
     */
    @RequestMapping(value = "/login.do",method = RequestMethod.POST)
    @ResponseBody
    public String login(@RequestParam(value = "userName") String mobile,
                        @RequestParam(value = "password") String password,
                        HttpServletResponse respone,HttpServletRequest request){
        User user= userFrontService.Login(mobile,password);
        request.getSession(true).setAttribute("loginUser",user);
//        request.getSession().setAttribute("loginUser",user);
        System.out.println("loginUser = " + request.getSession().getAttribute("loginUser"));
        Message message;
        if (user!=null){
        Set<Role> roleSet= user.getRoles();

        Map<String,Object> dataMap=new HashMap<>();
        dataMap.put("user",user);
        dataMap.put("role",roleSet);
            message=new Message(true,"login success",dataMap);
        }else{
            message=new Message(false,"login failed");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

    /**
     *
     * @param mobile
     * @param password
     * @return
     */
    @RequestMapping(value = "/register.do",method = RequestMethod.POST)
    @ResponseBody
    public String register(@RequestParam(value = "userName") String mobile,
                        @RequestParam(value = "password") String password,
                           HttpServletResponse respone){
        int userId= userFrontService.register(mobile, password);
        Message message;
        if (userId>0){
            Map<String,Object> map=new HashMap<>();
            map.put("userId",userId);
            message=new Message(true,"register success",map);
        }else if (userId==-1){
            message=new Message(false,"user has exit");
        }else{
            message=new Message(false,"register failed");
        }
//        respone.setHeader("Access-Control-Allow-Origin","*");
        return message.toJson();
    }

}
